
//
//  SettingView.m
//  mendianjieke
//
//  Created by 响当当 on 8/7/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "SettingView.h"
#import "NewLoginViewController.h"
#import "MySettingCell.h"
#import "MessageViewController.h"
#import <StoreKit/StoreKit.h>
#import "AboutUsViewController.h"

@interface SettingView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIView *footView;

@property (nonatomic, strong)NSMutableArray *setArr;
@property (nonatomic, strong)NSString *str;

@end

@implementation SettingView

- (NSMutableArray *)setArr
{
    if (!_setArr)
    {
        _setArr = [[NSMutableArray alloc]init];
        
        NSArray *arr = @[@{@"title":@"消息设置",@"subTitle":@""},
                         @{@"title":@"清除缓存",@"subTitle":@"0.2M"},
                         @{@"title":@"关于我们",@"subTitle":@""},
                         @{@"title":@"请给我们鼓励",@"subTitle":@""}];
        for (int i=0; i<arr.count; i++)
        {
            NSDictionary *dic = arr[i];
            MySettingModel *model = [[MySettingModel alloc]init];
            model.title = dic[@"title"];
            model.subTitle = dic[@"subTitle"];
            model.row = [NSString stringWithFormat:@"%d",i];
            [_setArr addObject:model];
        }
        
    }
    return _setArr;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT-50*kUIScaleBasePlus-NAVIBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (UIView *)footView
{
    if (!_footView)
    {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT-50*kUIScaleBasePlus-NAVIBAR_HEIGHT,SCREEN_WIDTH,50*kUIScaleBasePlus)];
        _footView.backgroundColor = [UIColor whiteColor];
        
        UIButton *logOutBtn = [Utilties creatButtonWithFrame:CGRectMake(0,0,SCREEN_WIDTH,50*kUIScaleBasePlus)
                                                textAligment:UIControlContentHorizontalAlignmentCenter
                                                        text:@"退出当前帐号"
                                                    textClor:LCHexColor(0x989898)
                                                   buttonBGC:[UIColor clearColor]
                                                    textFont:17*kUIScaleBasePlus
                                                      target:self
                                                      method:@selector(logOut)];
        [_footView addSubview:logOutBtn];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,1*kUIScaleBasePlus)];
        lineView.backgroundColor = JSALineColor;
        [_footView addSubview:lineView];
    }
    
    return _footView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.str = @"0";
        [self addSubview:self.tableView];
        [self addSubview:self.footView];
    }
    return self;
}


#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.setArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50*kUIScaleBasePlus;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5*kUIScaleBasePlus;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MySettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MySettingCell"];
    if (!cell)
    {
        cell = [[MySettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MySettingCell"];
        
        cell.selectionStyle = 0;
    }
    
    cell.model = self.setArr[indexPath.section];
    if ([self.str isEqualToString:@"1"])
    {
        [cell cleanSys];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            MessageViewController *vc = [[MessageViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [[Utilties getCurrentVC].navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            if([self.str isEqualToString:@"0"])
            {
                [self.tableView reloadData];
                self.str = @"1";
            }
        }
            break;
        case 2:
        {
            AboutUsViewController *vc = [[AboutUsViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [[Utilties getCurrentVC].navigationController pushViewController:vc animated:YES];
        }
                break;
        case 3:
        {
            if (@available(iOS 10.3, *)) {
                [SKStoreReviewController requestReview];
            }
        }
            break;
        default:
            break;
    }
}

- (void)logOut
{
    
    WeakSelf(self)
    [infoManager logOut:[NSDictionary new]
                 sucess:^(id  _Nonnull acceptData) {
                     current_User.hasLogin = @"0";
                     
                    [[Utilties getCurrentVC].navigationController popViewControllerAnimated:NO];
                        NewLoginViewController *vc = [[NewLoginViewController alloc]init];
                        kKeyWindow.rootViewController = vc;
                        [kKeyWindow.window makeKeyAndVisible];
                    }
                   fail:^(NSError * _Nullable error) {
                       
                   }];
}
@end
